package se.webbzon.boltzmann.effect;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldMovableObject;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.EllipsoidAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;

public class FireBombEffect extends WorldMovableObject implements Effect {
	
	// The maximal duration of the fire bomb effect
	private static final int maxDuration = 30;
	
	// The maximal speed of the fire bomb effect
	private static final double maxSpeed = 2;
	
	// The maximal radius of the fire bomb effect
	private final double maxRadius;
	
	// The startup duration of the fire bomb effect
	private final int duration;
	
	// The remaining duration of the fire bomb effect
	private int remaining; 

	/*============================================================
	Private Methods
	============================================================*/
	
	public FireBombEffect(WorldStaticObject parent, double maxRadius) {
		super(WorldObject.onStep, createAppearance());
		this.maxRadius = maxRadius * (0.5 + 0.5 * Math.random());
		duration = (int) (maxDuration * Math.random());
		remaining = duration;
		
		setPosition(parent.getX(),parent.getY(),parent.getZ());
		setSize(maxRadius,maxRadius,maxRadius);
		setVelocity(maxSpeed * (Math.random()-0.5),
					maxSpeed * (Math.random()-0.5),
					maxSpeed * (Math.random()-0.5));
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void onStep(WorldArea world) {
		if (remaining == 0)
			terminate(world);
		else if (remaining > 0) {
			final double radius = maxRadius * remaining / (double) duration;
			setSize(radius,radius,radius);
			remaining--;
		}
	}

	@Override public void launch(WorldArea world) {
		world.createInstance(this);
	}

	@Override public void terminate(WorldArea world) {
		world.destroyInstance(this);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	private static final Appearance3D createAppearance() {
		SimpleAppearance3D appearance = new EllipsoidAppearance3D();
		appearance.setTextureSet(Resources.getTextureSet("effect_lava"));
		return appearance;
	}

}
